
package com.wyzz.hr.base.action;

import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.*;
import org.springframework.web.util.HtmlUtils;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.bo.IEmployeeBo;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.util.BaseCrit;

public class AjaxEmpList extends BaseAction {

	public AjaxEmpList() {
	}

	public String execute() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String searchConditon = URLDecoder.decode(request
				.getParameter("searchConditon"), "UTF-8");
		String moduleNo = request.getParameter("moduleNo");
		Integer searchAll;
		try {
			searchAll = new Integer(request.getParameter("searchAll"));
		} catch (Exception e) {
			searchAll = null;
		}
		String beneType = request.getParameter("beneType");
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Employee.class);
		dc.createAlias(Employee.PROP_EMP_DEPT_NO, "empDeptNo", 1);
		String auth = null;
		if ("1".equals(moduleNo))
			auth = checkActionAuth("empQuitAdd", "addQuit");
		else if ("2".equals(moduleNo))
			auth = checkActionAuth("mySalaryPaid", "execute");
		else if ("4".equals(moduleNo))
			auth = checkActionAuth("allExaminSearch", "execute");
		else if ("3".equals(moduleNo))
			auth = checkActionAuth("trepList", "execute");
		else if ("6".equals(moduleNo))
			auth = checkActionAuth("resumeManage", "execute");
		else
			return null;
		if ("error".equals(auth))
			return null;
		if ("SUB".equals(auth))
			addSubDC(dc, "", Integer.parseInt(moduleNo));
		else if ("OWN".equals(auth))
			BaseCrit.addDC(dc, Employee.PROP_ID, "eq",
					new String[] { getCurrentEmp().getId() });
		BaseCrit.addDeptEmpDC(dc, null, searchConditon);
		BaseCrit.addDC(dc, Employee.PROP_EMP_STATUS, "eq",
				new Integer[] { searchAll });
		if ("1".equals(beneType))
			dc.add(Restrictions.isNotNull(Employee.PROP_EMP_BENEFIT_TYPE));
		dc.addOrder(Order.asc(Employee.PROP_EMP_DISTINCT_NO));
		IEmployeeBo empBo = (IEmployeeBo) getBean("empBo");
		List allSubEmps = empBo.findByCriteria(dc);
		StringBuffer xml = new StringBuffer();
		xml.append("<managers>\n");
		int len = allSubEmps.size();
		for (int i = 0; i < len; i++) {
			xml.append("<manager>\n");
			Employee emp = (Employee) allSubEmps.get(i);
			String id = emp.getEmpDistinctNo();
			String name = emp.getEmpName();
			String department = emp.getEmpDeptNo().getDepartmentName();
			String managerId = emp.getId();
			xml.append("<id>");
			xml.append(id);
			xml.append("</id>\n");
			xml.append("<name>");
			xml.append(HtmlUtils.htmlEscape(name));
			xml.append("</name>\n");
			xml.append("<department>");
			xml.append(HtmlUtils.htmlEscape(department));
			xml.append("</department>\n");
			xml.append("<managerId>");
			xml.append(managerId);
			xml.append("</managerId>\n");
			xml.append("</manager>\n");
		}

		xml.append("</managers>");
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/xml; charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		response.getWriter().write(xml.toString());
		return null;
	}

	private static final long serialVersionUID = 1L;
}
